package services

import (
	"errors"
	"fmt"
	"hft/Database/admin"
	"hft/Http/Models/dao"
	"hft/Http/Models/request"
	"hft/Http/Services/base"
	"hft/Http/Services/loupan/loupan_utils"
	utils "hft/Http/Utils"
	"hft/e"
	"hft/global"
	"hft/tools"
	"regexp"
)

type LouPanSwiper struct{}

func (*LouPanSwiper) CreateLouPanSwiper(claims tools.BaseClaims, data *admin.LouPanSwiper) (err error) {

	var loupan *admin.LouPan

	var category *admin.LouPanSwiperCategory

	account, err := base.FindAccount(claims, global.OC_DB)

	if err != nil {
		return errors.New(e.AccountNotExist)
	}

	err = global.OC_DB.First(&loupan, data.LouPanID).Error

	if err != nil {
		return errors.New("找不到该楼盘信息")
	}

	err = global.OC_DB.First(&category, data.Category).Error

	if err != nil {
		return errors.New("找不到该相册分类信息")
	}

	match, err := regexp.MatchString(fmt.Sprintf("(%v)", account.RegionLink), loupan.RegionLink)

	if err != nil {
		return errors.New("未知错误500")
	}

	if !match {
		return errors.New("你无权添加该楼盘相册")
	}

	data.CategoryName = category.Name

	err = global.OC_DB.Create(&data).Error

	if err != nil {
		return errors.New("未知错误")
	}

	return nil
}

func (*LouPanSwiper) EditLouPanSwiper(claims tools.BaseClaims, data *admin.LouPanSwiper) (err error) {

	var loupan *admin.LouPan

	var category *admin.LouPanSwiperCategory

	err = global.OC_DB.First(&admin.LouPanSwiper{}, data.ID).Error

	if err != nil {
		return errors.New("找不到该相册信息")
	}

	account, err := base.FindAccount(claims, global.OC_DB)
	if err != nil {
		return errors.New(e.AccountNotExist)
	}

	err = global.OC_DB.First(&loupan, data.LouPanID).Error

	if err != nil {
		return errors.New("找不到该楼盘信息")
	}

	err = global.OC_DB.First(&category, data.Category).Error

	if err != nil {
		return errors.New("找不到该相册分类信息")
	}

	match, err := regexp.MatchString(fmt.Sprintf("(%v)", account.RegionLink), loupan.RegionLink)

	if err != nil {
		return errors.New("未知错误500")
	}

	if !match {
		return errors.New("你无权添加该楼盘相册")
	}

	data.CategoryName = category.Name

	if category.IsCover {
		err = global.OC_DB.Where("id = ?", data.ID).Omit("create_time,update_time,deleted_at,category").Save(&data).Error
	} else {
		err = global.OC_DB.Where("id = ?", data.ID).Omit("create_time,update_time,deleted_at,category,go_url").Save(&data).Error
	}

	if err != nil {
		return errors.New("未知错误")
	}

	return nil
}

func (*LouPanSwiper) GetSwiperList(claims tools.BaseClaims, paginate *request.LouPanSwiperSearch) (data *utils.Page, err error) {

	var loupan *admin.LouPan

	var swipers []*admin.LouPanSwiper

	if paginate.LouPanId == 0 {
		return nil, errors.New("无效的楼盘信息")
	}

	account, err := base.FindAccount(claims, global.OC_DB)

	if err != nil {
		return nil, errors.New(e.AccountNotExist)
	}

	err = global.OC_DB.First(&loupan, paginate.LouPanId).Error

	if err != nil {
		return nil, errors.New("找不到该楼盘信息")
	}

	match, err := regexp.MatchString(fmt.Sprintf("(%v)", account.RegionLink), loupan.RegionLink)

	if err != nil {
		return nil, errors.New("未知错误500")
	}

	if !match {
		return nil, errors.New("你无权获取该楼盘相关相册信息")
	}

	err = global.OC_DB.Scopes(dao.LouPanSwiperDao.GetSwiperList(paginate, global.OC_DB)).Find(&swipers).Offset(-1).Limit(-1).Count(&paginate.Total).Error

	if err != nil {
		return nil, errors.New("未知错误500")
	}

	paginate.Data = swipers

	return &paginate.Page, nil
}

func (*LouPanSwiper) Delete(louPanID int, ids []int, claims tools.BaseClaims) (err error) {

	account, err := base.FindAccount(claims, global.OC_DB)

	if err != nil {
		return errors.New(e.AccountNotExist)
	}
	var lou *admin.LouPan
	err = global.OC_DB.First(&lou, louPanID).Error
	if err != nil {
		return errors.New(e.LouPanNotExist)
	}
	match, _ := regexp.MatchString(fmt.Sprintf("^%v", account.RegionLink), lou.RegionLink)
	if !match {
		return errors.New(e.NoRightToOpera)
	}
	var swipers []*admin.LouPanSwiper
	global.OC_DB.Find(&swipers, ids)
	if !loupan_utils.IsTheseSwiperBelongThisLou(swipers, lou.ID) {
		return errors.New(e.NoRightToOpera)
	}
	global.OC_DB.Unscoped().Delete(&swipers)
	return nil
}

var LouPanSwiperService = new(LouPanSwiper)
